/* c_sims.do - ****************************************************************

	Conduct analysis of SIMS outcomes used in:
    - Table 4 (grade progression)
	  - Table 7 (non-cognitive outcomes)
		- Table 9 (School quality and choice, peer characteristics)
		- Appendix Table A11 (Grade repetition and SPED status)

******************************************************************************/

set matsize 11000

use "$stata_data_analysis/final_sample.dta", clear
drop hs_num_repeat_grade_asgn_tot_9
order *, sequential

local final_controls hispanic female black white sp_biling
local final_outcomes ontime_asgn1 enrolled6 enrolled9 ever_repeat_grade_asgn any_spd* ///
	grad_hs_ontime grad_hs /// Table 4 outcomes
	mid_ever_anysusp mid_num_anysusp mid_ever_truancy mid_num_anysusp_tot mid_num_truancy ///
	mid_num_truancy_tot mid_num_num_absences mid_num_num_absences_tot mid_ever_incarcerated ms_behav_index_tot ///
	hs_ever_anysusp hs_num_anysusp_tot hs_ever_truancy hs_num_truancy_tot hs_num_num_absences_tot ///
	hs_ever_incarcerated behav_index_tot /// Table 7 outcomes
	VAMtest_to_BPS4 VAMtest_to_BPS5 VAMtest_to_BPS6 VAMtest_to_BPS7 ///
	VAMtest_to_BPS8 VAMtest_to_BPS10 VAMtest_to_BPS_avg ///
	exam* charter* ///
	meaneverany_nsc* meanblack* meanfrpl* meanhsgrad* meanprek* ///
	meanstd_e4*	meanstd_e5* meanstd_e6* meanstd_e7* meanstd_e8* meanstd_e10* ///
	meanstd_m4*	meanstd_m5* meanstd_m6* meanstd_m7* meanstd_m8* meanstd_m10* /// Table 9 outcomes
	repeat_grade_asgn* // Table A11

forval i = 1/3 {
*Conduct progression analysis with full demographic controls.
if `i' == 1 {
	local endog				att
	local instrument 		soffany
	local controls			"i.yr_asgn `final_controls'"
	local pscore			"pformany"
	local outcomes			`final_outcomes'
	local restriction		"in_K1 & in_sims"
	local suffix 			"an_fr_K1_prog_cntrl"
	local demographic_list 	""
}
*Conduct subgroup analysis of progression results (no controls)
if `i' == 2 {
	local endog				att
	local instrument 		soffany
	local controls			"i.yr_asgn"
	local pscore			"pformany"
	local outcomes			`final_outcomes'
	local restriction		"in_K1 & in_sims"
	local suffix 			"an_fr_K1_prog"
	local demographic_list 	"hisp asian white black fem male adv urm not_frpl frpl inc1 inc2 inc3 inc4"
}
*Specification with linear p-score controls and no other covariate controls, besides year
if `i' == 3 {
	local endog				att
	local instrument 		soffany
	local controls			"i.yr_asgn pformany"
	local outcomes			`final_outcomes'
	local restriction		"in_K1 & in_sims & !inlist(pformany, 0, 1)"
	local suffix			"an_fr_K1_lin_cntrl"
	local demographic_list 	"hisp asian black white fem male not_frpl frpl inc1 inc2 inc3 inc4"
}

if `i' == 3 {
	* linear pscore version
	preserve
		get_estimates, endogvar(`endog') instruments(`instrument') idvar(studentno) ///
		outcomes(`outcomes') other_controls(`controls') restriction(`restriction') ///
		outfile("$results/tables/sims`suffix'") 2sls(1) rf(1) fs(1) non_offered_mean(1)
	restore

}
else {
	* saturated pscore version
	preserve
		get_estimates, endogvar(`endog') instruments(`instrument') idvar(studentno) ///
		outcomes(`outcomes') other_controls(`controls') restriction(`restriction') pscore(`pscore') ///
		outfile("$results/tables/sims`suffix'") 2sls(1) rf(1) fs(1) non_offered_mean(1)
	restore
}

* Sub-group analysis
foreach demo of local demographic_list {
	if "`demo'" == "hisp" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & hispanic == 1"
	}
	if "`demo'" == "asian" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & asian == 1"
	}
	if "`demo'" == "black" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & black == 1"
	}
	if "`demo'" == "fem" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & female == 1"
	}
	if "`demo'" == "male" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & female == 0"
	}
	if "`demo'" == "adv" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & (white == 1 | asian == 1)"
	}
	if "`demo'" == "urm" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & white != 1 & asian != 1"
	}
	if "`demo'" == "white" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & white == 1"
	}
	if "`demo'" == "frpl" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & frpl == 1"
	}
	if "`demo'" == "not_frpl" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & not_frpl == 1"
	}
	if inlist("`demo'","inc1" , "inc2","inc3","inc4") {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & `demo' == 1"
	}


	if `i' == 2 {
		preserve
			get_estimates, endogvar(`endog') instruments(`instrument') idvar(studentno) ///
			outcomes(`outcomes') other_controls(`controls') restriction(`restriction_new') pscore(`pscore') ///
			outfile("$results/tables/sims_`suffix_new'") 2sls(1) rf(1) fs(1) non_offered_mean(1)
		restore
	}

}


}
